% evaluates moment conditions of the form E[h(x,r)] = p 
% aJ is a vector of zeros and ones that selects moments to be evaluated
% set aJ = 0 to use S matrix as covar of pricing errors in case where no
% parameters are estimated (in this case the function always uses all moments) 

function [g] = GMMcritg(gtfcn,b,x,r,p,W,h,Sfcn,aJ,wt)


t = size(r,1);
if nargin > 7 && size(aJ,1) > 1    % size(aJ,1) == 1 : no SDF parameters estimated
    fast = 0; 
else
    fast = 1; 
end

if nargin < 7
    h = 0; 
end 

[gt, dg] = gtfcn(b,x,r,p,h,fast); 
g = mean(gt);

